import { IsArray, IsDefined, IsNotEmpty, IsString, Length, ValidateNested } from "class-validator";
import { BaseEntity } from "../BaseEntity";
import { TStatus } from "../../types/CommonType";
import { Model } from "sequelize";
import SysDictDataEntity from "./SysDictDataEntity";

export default class SysDictTypeEntity extends BaseEntity {
  public id?: string;

  @Length(2, 20)
  @IsString()
  @IsNotEmpty()
  @IsDefined()
  public dictName: string;

  @Length(2, 20)
  @IsString()
  @IsNotEmpty()
  @IsDefined()
  public dictType: string;

  @ValidateNested()
  @IsArray()
  @IsNotEmpty()
  @IsDefined()
  public dictData?: SysDictDataEntity[];

  @IsString()
  @IsNotEmpty()
  @IsDefined()
  public status: TStatus;

  @IsString()
  public remark: string;

  public static transform(plainObject: object): SysDictTypeEntity {
    const result = super.baseTransform(SysDictTypeEntity, plainObject);
    if (Array.isArray(result.dictData)) {
      result.dictData = result.dictData.map((item) => super.baseTransform(SysDictDataEntity, item));
    }
    return result;
  }
}

export interface ISysDictTypeModel extends Model<SysDictTypeEntity> {
  [x: string]: any;
  setDictDataTab: (data: any) => Promise<void>;
  getDictDataTab: <T>() => Promise<T>;
}
